# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'mainwindow.ui'
#
# Created: Wed Oct 19 20:06:36 2016
#      by: pyside-uic 0.2.15 running on PySide 1.2.2
#
# WARNING! All changes made in this file will be lost!

from PySide import QtCore, QtGui
import VRC4FPython as v4
import os
from subsector import subsector_type1
from vlogic_analyze import process_type3
from visualize import hist2d_plot,hist2d_plot_three_plot_type1  
from copy import copy
import sys
import pdb
from utils import right_shift,string_xor


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(572, 300)
        self.centralWidget = QtGui.QWidget(MainWindow)
        self.centralWidget.setObjectName("centralWidget")
        self.pushButton_p0q0 = QtGui.QPushButton(self.centralWidget)
        self.pushButton_p0q0.setGeometry(QtCore.QRect(20, 200, 51, 23))
        self.pushButton_p0q0.setObjectName("pushButton_p0q0")
        self.pushButton_p1q1 = QtGui.QPushButton(self.centralWidget)
        self.pushButton_p1q1.setGeometry(QtCore.QRect(80, 170, 51, 23))
        self.pushButton_p1q1.setObjectName("pushButton_p1q1")
        self.pushButton_p2q1 = QtGui.QPushButton(self.centralWidget)
        self.pushButton_p2q1.setGeometry(QtCore.QRect(140, 170, 51, 23))
        self.pushButton_p2q1.setObjectName("pushButton_p2q1")
        self.pushButton_p3q1 = QtGui.QPushButton(self.centralWidget)
        self.pushButton_p3q1.setGeometry(QtCore.QRect(200, 170, 51, 23))
        self.pushButton_p3q1.setObjectName("pushButton_p3q1")
        self.pushButton_p4q1 = QtGui.QPushButton(self.centralWidget)
        self.pushButton_p4q1.setGeometry(QtCore.QRect(260, 170, 51, 23))
        self.pushButton_p4q1.setObjectName("pushButton_p4q1")
        self.pushButton_p5q1 = QtGui.QPushButton(self.centralWidget)
        self.pushButton_p5q1.setGeometry(QtCore.QRect(320, 170, 51, 23))
        self.pushButton_p5q1.setObjectName("pushButton_p5q1")
        self.pushButton_p4q2 = QtGui.QPushButton(self.centralWidget)
        self.pushButton_p4q2.setGeometry(QtCore.QRect(260, 140, 51, 23))
        self.pushButton_p4q2.setObjectName("pushButton_p4q2")
        self.pushButton_p4q3 = QtGui.QPushButton(self.centralWidget)
        self.pushButton_p4q3.setGeometry(QtCore.QRect(260, 110, 51, 23))
        self.pushButton_p4q3.setObjectName("pushButton_p4q3")
        self.pushButton_p4q4 = QtGui.QPushButton(self.centralWidget)
        self.pushButton_p4q4.setGeometry(QtCore.QRect(260, 80, 51, 23))
        self.pushButton_p4q4.setObjectName("pushButton_p4q4")
        self.pushButton_p6q1 = QtGui.QPushButton(self.centralWidget)
        self.pushButton_p6q1.setGeometry(QtCore.QRect(380, 170, 51, 23))
        self.pushButton_p6q1.setObjectName("pushButton_p6q1")
        self.pushButton_p7q1 = QtGui.QPushButton(self.centralWidget)
        self.pushButton_p7q1.setGeometry(QtCore.QRect(440, 170, 51, 23))
        self.pushButton_p7q1.setObjectName("pushButton_p7q1")
        self.pushButton_p8q0 = QtGui.QPushButton(self.centralWidget)
        self.pushButton_p8q0.setGeometry(QtCore.QRect(500, 200, 51, 23))
        self.pushButton_p8q0.setObjectName("pushButton_p8q0")
        self.radioButton = QtGui.QRadioButton(self.centralWidget)
        self.radioButton.setGeometry(QtCore.QRect(20, 10, 89, 16))
        self.radioButton.setObjectName("radioButton")
        self.pushButton = QtGui.QPushButton(self.centralWidget)
        self.pushButton.setGeometry(QtCore.QRect(10, 40, 75, 23))
        self.pushButton.setObjectName("pushButton")
        self.pushButton_p2q2 = QtGui.QPushButton(self.centralWidget)
        self.pushButton_p2q2.setGeometry(QtCore.QRect(140, 140, 51, 23))
        self.pushButton_p2q2.setObjectName("pushButton_p2q2")
        self.pushButton_p3q2 = QtGui.QPushButton(self.centralWidget)
        self.pushButton_p3q2.setGeometry(QtCore.QRect(200, 140, 51, 23))
        self.pushButton_p3q2.setObjectName("pushButton_p3q2")
        self.pushButton_p3q3 = QtGui.QPushButton(self.centralWidget)
        self.pushButton_p3q3.setGeometry(QtCore.QRect(200, 110, 51, 23))
        self.pushButton_p3q3.setObjectName("pushButton_p3q3")
        self.pushButton_p5q2 = QtGui.QPushButton(self.centralWidget)
        self.pushButton_p5q2.setGeometry(QtCore.QRect(320, 140, 51, 23))
        self.pushButton_p5q2.setObjectName("pushButton_p5q2")
        self.pushButton_p5q3 = QtGui.QPushButton(self.centralWidget)
        self.pushButton_p5q3.setGeometry(QtCore.QRect(320, 110, 51, 23))
        self.pushButton_p5q3.setObjectName("pushButton_p5q3")
        self.pushButton_p6q2 = QtGui.QPushButton(self.centralWidget)
        self.pushButton_p6q2.setGeometry(QtCore.QRect(380, 140, 51, 23))
        self.pushButton_p6q2.setObjectName("pushButton_p6q2")
        MainWindow.setCentralWidget(self.centralWidget)
        self.menuBar = QtGui.QMenuBar(MainWindow)
        self.menuBar.setGeometry(QtCore.QRect(0, 0, 572, 23))
        self.menuBar.setObjectName("menuBar")
        MainWindow.setMenuBar(self.menuBar)
        self.mainToolBar = QtGui.QToolBar(MainWindow)
        self.mainToolBar.setObjectName("mainToolBar")
        MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.mainToolBar)
        self.statusBar = QtGui.QStatusBar(MainWindow)
        self.statusBar.setObjectName("statusBar")
        MainWindow.setStatusBar(self.statusBar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton_p0q0.setText(QtGui.QApplication.translate("MainWindow", "p0q0", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton_p1q1.setText(QtGui.QApplication.translate("MainWindow", "p1q1", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton_p2q1.setText(QtGui.QApplication.translate("MainWindow", "p2q1", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton_p3q1.setText(QtGui.QApplication.translate("MainWindow", "p3q1", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton_p4q1.setText(QtGui.QApplication.translate("MainWindow", "p4q1", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton_p5q1.setText(QtGui.QApplication.translate("MainWindow", "p5q1", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton_p4q2.setText(QtGui.QApplication.translate("MainWindow", "p4q2", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton_p4q3.setText(QtGui.QApplication.translate("MainWindow", "p4q3", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton_p4q4.setText(QtGui.QApplication.translate("MainWindow", "p4q4", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton_p6q1.setText(QtGui.QApplication.translate("MainWindow", "p6q1", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton_p7q1.setText(QtGui.QApplication.translate("MainWindow", "p7q1", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton_p8q0.setText(QtGui.QApplication.translate("MainWindow", "p8q0", None, QtGui.QApplication.UnicodeUTF8))
        self.radioButton.setText(QtGui.QApplication.translate("MainWindow", "xor", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton.setText(QtGui.QApplication.translate("MainWindow", "go", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton_p2q2.setText(QtGui.QApplication.translate("MainWindow", "p2q2", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton_p3q2.setText(QtGui.QApplication.translate("MainWindow", "p3q2", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton_p3q3.setText(QtGui.QApplication.translate("MainWindow", "p3q3", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton_p5q2.setText(QtGui.QApplication.translate("MainWindow", "p5q2", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton_p5q3.setText(QtGui.QApplication.translate("MainWindow", "p5q3", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton_p6q2.setText(QtGui.QApplication.translate("MainWindow", "p6q2", None, QtGui.QApplication.UnicodeUTF8))


class ControlMainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(ControlMainWindow, self).__init__(parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        # 函数绑定
        self.ui.pushButton_p0q0.clicked.connect(self.button_click_p0q0)
        self.ui.pushButton_p8q0.clicked.connect(self.button_click_p8q0)
        self.ui.pushButton_p1q1.clicked.connect(self.button_click_p1q1)
        self.ui.pushButton_p2q1.clicked.connect(self.button_click_p2q1)
        self.ui.pushButton_p3q1.clicked.connect(self.button_click_p3q1)
        self.ui.pushButton_p4q1.clicked.connect(self.button_click_p4q1)
        self.ui.pushButton_p5q1.clicked.connect(self.button_click_p5q1)
        self.ui.pushButton_p6q1.clicked.connect(self.button_click_p6q1)
        self.ui.pushButton_p7q1.clicked.connect(self.button_click_p7q1)
        self.ui.pushButton_p2q2.clicked.connect(self.button_click_p2q2)
        self.ui.pushButton_p3q2.clicked.connect(self.button_click_p3q2)
        self.ui.pushButton_p4q2.clicked.connect(self.button_click_p4q2)
        self.ui.pushButton_p5q2.clicked.connect(self.button_click_p5q2)
        self.ui.pushButton_p6q2.clicked.connect(self.button_click_p6q2)
        self.ui.pushButton_p3q3.clicked.connect(self.button_click_p3q3)
        self.ui.pushButton_p4q3.clicked.connect(self.button_click_p4q3)
        self.ui.pushButton_p5q3.clicked.connect(self.button_click_p5q3)
        self.ui.pushButton_p4q4.clicked.connect(self.button_click_p4q4)
        self.ui.pushButton.clicked.connect(self.button_go_click)

        self.pq_dict = {}
        self.target_files = []
    def CreateCypherStream(self,p,q,size,key,fileName):
        v4.PyVLRC4SaveKeyStreamToFile_SingleSeed(key=key,fileName=fileName,p=p,q=q,size=size)

    def xorfile(self):
        pass

    def caculate_button(self,p,q):
        tt = "p%dq%d" % (p,q)
        button_name = "self.ui.pushButton_p%dq%d" % (p,q)
        target_button = eval(button_name)
        return target_button
    def change_background(self,p,q):
        tt = "p%dq%d" % (p,q)
        button_name = "self.ui.pushButton_p%dq%d" % (p,q)
        target_button = eval(button_name)
        print self.pq_dict.has_key(tt)
        if self.pq_dict.has_key(tt):
            self.pq_dict.__delitem__(tt);
            target_button.setStyleSheet("background-color: rgb(225, 225, 225);");
        else:
            self.pq_dict[tt] = (p,q)
            target_button.setStyleSheet("background-color: rgb(170, 0, 255);");


    def button_click_p0q0(self):
        self.change_background(0,0)
    def button_click_p8q0(self):
        self.change_background(8,0)
        # if self.pq_dict.has_key("p0q1"):
        #     self.pq_dict.__delitem__("p0q1");
        #     self.ui.pushButton_p0q0.setStyleSheet("background-color: rgb(225, 225, 225);");
        # else:
        #     self.pq_dict["p0q1"] = (0,1)
        #     self.ui.pushButton_p0q0.setStyleSheet("background-color: rgb(170, 0, 255);");
    def button_click_p1q1(self):
        self.change_background(1,1)
    def button_click_p2q1(self):
        self.change_background(2,1)
    def button_click_p3q1(self):
        self.change_background(3,1)
    def button_click_p4q1(self):
        self.change_background(4,1)
    def button_click_p5q1(self):
        self.change_background(5,1)
    def button_click_p6q1(self):
        self.change_background(6,1)
    def button_click_p7q1(self):
        self.change_background(7,1)
    def button_click_p8q1(self):
        self.change_background(8,1)
    def button_click_p2q2(self):
        self.change_background(2,2)
    def button_click_p3q2(self):
        self.change_background(3,2)
    def button_click_p4q2(self):
        self.change_background(4,2)
    def button_click_p5q2(self):
        self.change_background(5,2)
    def button_click_p6q2(self):
        self.change_background(6,2)
    def button_click_p7q2(self):
        self.change_background(7,2)
    def button_click_p3q3(self):
        self.change_background(3,3)
    def button_click_p4q3(self):
        self.change_background(4,3)
    def button_click_p5q3(self):
        self.change_background(5,3)
    def button_click_p4q4(self):
        self.change_background(4,4)
    def button_go_click(self):
        print self.pq_dict.values()

        base_dir =  os.path.split(os.path.realpath(__file__))[0]
        # 清空文件夹的数据
        self.target_files = []
        targetDir = base_dir+"/data/"
        for  file in os.listdir(targetDir):
            targetFile = os.path.join(targetDir,  file) 
            if os.path.isfile(targetFile):
                os.remove(targetFile)
        
        # 生成数据
        if not self.ui.radioButton.isChecked():
            pArray = []
            qArray = []
            for (p,q) in self.pq_dict.values():
                pArray.append(p)
                qArray.append(q)
            fileName = "\\data\\VLRC4_p%sq%s.char" % (pArray,qArray)
            v4.PyVLRC4SaveKeyStreamToFile_MultiSeed(pArray=pArray,qArray=qArray,size=2500,key="house.young",fileName=base_dir+fileName)
        else:
            for (p,q) in self.pq_dict.values():
                fileName = "\\data\\VLRC4_p%sq%s.char" % (p,q)
                self.target_files.append(fileName)
                v4.PyVLRC4SaveKeyStreamToFile_SingleSeed(key="house.young",fileName=base_dir+fileName,p=p,q=q,size=2500)



        if self.ui.radioButton.isChecked():
            # 加载并进行异或操作
            r_string = ""
            is_next = False
            for file in self.target_files:
                string = open(base_dir+file,"rb").read()
                if is_next:
                    r_string = string_xor(r_string,string)
                else:
                    r_string = string
                    is_next = True
            # 将结构保存到文件当中
            import pdb;pdb.set_trace()
            open(base_dir+"/data/xorresult.char","wb").write(r_string)
        else:
            r_string = open(base_dir+fileName,"rb").read()

        size =8
        x_range=(0,size)
        y_range = (0,size)
        p_array=[]
        q_array=[]


        subsection = subsector_type1(r_string,size)
        for coordinate in subsection:
            if coordinate[0] < coordinate[1]:
                (p, q) = process_type3(r_string[coordinate[0]:coordinate[1]+1])
                # pdb.set_trace()
                p_array.append(copy(p))
                q_array.append(copy(q))
        # hist2d_plot(q_array,p_array, x_range, y_range,"house")
        # print p_array
        # print q_array
        hist2d_plot_three_plot_type1(p_array,q_array,"")






def IsSubString(SubStrList,Str):  
    ''''' 
    #判断字符串Str是否包含序列SubStrList中的每一个子字符串 
    #>>>SubStrList=['F','EMS','txt'] 
    #>>>Str='F06925EMS91.txt' 
    #>>>IsSubString(SubStrList,Str)#return True (or False) 
    '''  
    flag=True  
    for substr in SubStrList:    
        if not(substr in Str):  
            flag=False  
  
    return flag  

def GetFileList(FindPath,FlagStr=[]):  
    '''''  
    #获取目录中指定的文件名 
    #>>>FlagStr=['F','EMS','txt'] #要求文件名称中包含这些字符 
    #>>>FileList=GetFileList(FindPath,FlagStr) # 
    '''  
    import os  
    FileList=[]  
    FileNames=os.listdir(FindPath)  
    if (len(FileNames)>0):  
       for fn in FileNames:  
           if (len(FlagStr)>0):  
               #返回指定类型的文件名  
               if (IsSubString(FlagStr,fn)):  
                   fullfilename=os.path.join(FindPath,fn)  
                   FileList.append(fullfilename)  
           else:  
               #默认直接返回所有文件名  
               fullfilename=os.path.join(FindPath,fn)  
               FileList.append(fullfilename)  
  
    #对文件名排序  
    if (len(FileList)>0):  
        FileList.sort()  
  
    return FileList  


import sys


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    mySW = ControlMainWindow()
    mySW.show()
    sys.exit(app.exec_())